package com.codeanalysissys.backend.python;
import java.util.ArrayList;
import java.util.Objects;

public class PythonClass {

    public final String name;
    public final String filePath;
    public final String[] exTents;
    public final String[] ImPorts;
    private String typeParameters;
    private ArrayList<PythonField> fields;
    private ArrayList<PythonMethod> methods;

    public PythonClass(String name, String filePath, String[] imports, String[] exTents) {
        this.ImPorts = imports;
        this.name = name;
        this.filePath = filePath;
        this.exTents = exTents;
        fields = new ArrayList<>();
        methods = new ArrayList<>();
    }

    public void setTypeParameters(String params){
        this.typeParameters = params;
    }

    public String getTypeParameters(){
        return this.typeParameters;
    }
    public void addField(PythonField field) {
        fields.add(field);
    }
    public boolean hasField(String name) {
        for (PythonField fld : fields) {
            if (fld.NAME.equals(name)) {
                return true;
            }
        }
        return false;
    }

    public PythonField[] getAllFields() {
        return fields.toArray(new PythonField[fields.size()]);
    }

    public void addMethod(PythonMethod method) {
        methods.add(method);
    }

    public boolean hasMethod(String name) {
        for (PythonMethod mtd : methods) {
            if (mtd.NAME.equals(name)) {
                return true;
            }
        }
        return false;
    }

    public PythonMethod[] getAllMethods() {
        return methods.toArray(new PythonMethod[methods.size()]);
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder("CLASS \n{\n");
        str.append("  NAME : \"").append(name).append("\",\n");
        str.append("  FILE : \"").append(filePath).append("\",\n");
        str.append("  FIELDS : \n  [\n");
        for (PythonField fld : fields) {
            str.append("    ").append(fld).append(",\n");
        }
        str.append("  ],\n");
        str.append("  METHODS : \n  [\n");
        for (PythonMethod mtd : methods) {
            str.append("    ").append(mtd).append(",\n");
        }
        str.append("  ]\n");
        str.append("}");
        return str.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof PythonClass)) {
            return false;
        }
        PythonClass cls = (PythonClass) obj;
        return this.name.equals(cls.name);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(name);
    }
}
